package thread;
/**
 * 实现一个多线程(多个售票窗口)卖票程序
 * 构建3个线程，卖共同的10张票
 */
class TicketTask implements Runnable {
    //临界资源(多线程共享资源)
    int count = 10;
    public void run()
    {
        while (true)
           synchronized (this) {//同步代码块，这里的this可以看成是对象锁(同步锁)
           {
                if (count <= 0) break;
                System.out.println(Thread.currentThread().getName() + "卖了第" + count + "张票");
                count--;
           }
        }
    }
}
public class TicketDemo01 {
    public static void main(String[] args) {
        //1.构建售票任务对象
        TicketTask ticketTask = new TicketTask();
        Thread thread1 = new Thread(ticketTask, "窗口1");
        Thread thread2 = new Thread(ticketTask, "窗口2");
        Thread thread3 = new Thread(ticketTask, "窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
